package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.userInfo.entity.CollectedArticle;

import java.util.List;

public class CollectedSerializeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //context
    private Context mContext = null;
    //list
    private List<CollectedArticle.ResponseBean> collectedArticles;
    //type
    public static final int ARTICLE_HEADER = 0;
    public static final int ARTICLE_CONTENT = 1;
    //interface
    private OnItemFunctionClickListener mOnItemFunctionClickListener = null;
    //文集sum
    private int collectedArticleSum = 0;

    public CollectedSerializeAdapter(Context context) {
        this.mContext = context;
    }

    public void setCollectedArticles(List<CollectedArticle.ResponseBean> list) {
        this.collectedArticles = list;
    }

    public void addCollectedArticles(List<CollectedArticle.ResponseBean> list) {
        this.collectedArticles.addAll(list);
    }

    public void setCollectedArticleSum(int sum) {
        this.collectedArticleSum = sum;
    }

    public List<CollectedArticle.ResponseBean> getCollectedArticles() {
        return this.collectedArticles;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == ARTICLE_HEADER) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_collected_article_header_view, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_collected_serialize_item_view, parent, false);
            return new CollectedSerializeViewHolder(view);
        }
        return new CollectedSerializeHeaderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CollectedSerializeViewHolder) {
            //因头部，position-1
            final int index = position - 1;
            //数据
            final CollectedArticle.ResponseBean responseBean = this.collectedArticles.get(index);
            CollectedSerializeViewHolder viewHolder = (CollectedSerializeViewHolder) holder;
            //文集title
            viewHolder.txt_article_title.setText(responseBean.getTitle());
            //文集状态
            final int is_check = responseBean.getIs_check();
            viewHolder.txt_article_status.setText(is_check == 1 ?
                    "已发布" : is_check == 2 ?
                    "审核中" : is_check == 3 ?
                    "草稿" : "");
            //热度，字数
            viewHolder.txt_article_heat.setText("字数" + responseBean.getNumber());
            //日期
            viewHolder.txt_article_data.setText(DateUtils.ConvertDate(responseBean.getCheck_datetime()));
            viewHolder.layout_collected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemFunctionClickListener != null) {
                        mOnItemFunctionClickListener.onClick(responseBean.getContribute_id());
                    }
                }
            });
            viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemFunctionClickListener != null) {
                        mOnItemFunctionClickListener.onDeleteClick(responseBean.getContribute_id(), is_check);
                    }
                }
            });
        } else if (holder instanceof CollectedSerializeHeaderViewHolder) {
            CollectedSerializeHeaderViewHolder viewHolder = (CollectedSerializeHeaderViewHolder) holder;
            //sum
            viewHolder.txt_article_sum.setText("(共" + collectedArticleSum + "篇)");
            //img
            viewHolder.iv_select.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        if (collectedArticles == null) {
            return 1;
        }
        return collectedArticles.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ARTICLE_HEADER;
        } else {
            return ARTICLE_CONTENT;
        }
    }

    public static class CollectedSerializeViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout layout_collected;

        private TextView txt_article_title, txt_article_status, txt_article_heat, txt_article_data;

        private Button btn_delete;

        public CollectedSerializeViewHolder(View itemView) {
            super(itemView);
            layout_collected = itemView.findViewById(R.id.layout_collected);
            txt_article_title = itemView.findViewById(R.id.txt_article_title);
            txt_article_status = itemView.findViewById(R.id.txt_article_status);
            txt_article_heat = itemView.findViewById(R.id.txt_article_heat);
            txt_article_data = itemView.findViewById(R.id.txt_article_data);
            btn_delete = itemView.findViewById(R.id.btn_delete);
        }
    }

    public static class CollectedSerializeHeaderViewHolder extends RecyclerView.ViewHolder {

        private TextView txt_article_sum;

        private ImageView iv_select;

        public CollectedSerializeHeaderViewHolder(View itemView) {
            super(itemView);
            txt_article_sum = itemView.findViewById(R.id.txt_article_sum);
            iv_select = itemView.findViewById(R.id.iv_select);
        }
    }


    public void setOnItemFunctionClickListener(OnItemFunctionClickListener listener) {
        mOnItemFunctionClickListener = listener;
    }

    //interface
    public interface OnItemFunctionClickListener {

        void onClick(int contribute_id);

        void onDeleteClick(int contribute_id, int is_check);
    }
}
